 # -*- coding: UTF-8 -*- 
"""
Python 3.6.8 64bit centos 7.9

"""
import subprocess,sys,mylib_utf8_202406017
from pathlib import Path
from PIL import Image

# 替换为你的可执行文件路径
# executable_path = Path(__file__).parent / 'testsdk'
executable_path="./testsdk"
executable_path = str(executable_path)
print(executable_path)

# 定义testsdk需要的参数

# pic_path = '/home/ubuntu/Desktop/tspl_Printer/sample-color.bmp'
# ipaddress = '192.168.1.1'
# size1 = '100'
# size2 = '200'
# gap1 = '10'
# gap2 = '20'
# direction = '1'  # 假设方向参数是 'N', 'S', 'E', 或 'W' 中的一个
# BMPcoordinate1 = '10'
# BMPcoordinate2 = '200'

pic_path = sys.argv[1]
scale_factor= float(sys.argv[2])
ipaddress = sys.argv[3]
size1 = sys.argv[4]
size2 = sys.argv[5]
gap1 = sys.argv[6]
gap2 = sys.argv[7]
direction = sys.argv[8]
BMPcoordinate1 = sys.argv[9]
BMPcoordinate2 = sys.argv[10]


# 构建参数列表
args = [executable_path,ipaddress, size1, size2, gap1, gap2, direction, BMPcoordinate1, BMPcoordinate2 ]

# 把图片转换为黑白位图
def color2bw(PICdirectory,scale_factor):
    # Open the image file
    image = Image.open(PICdirectory)

    # Convert the image to grayscale
    grayscale_image = image.convert('1')

     # 获取原始图片的宽度和高度
    original_width, original_height = grayscale_image.size
    # print(f"Original size: {original_width}x{original_height}")
    # 计算缩放后的宽度和高度
    new_width = int(original_width * scale_factor)
    new_height = int(original_height * scale_factor)
    # print(f"Resized size: {new_width}x{new_height}")
    # 缩放图片
    resized_img = grayscale_image.resize((new_width, new_height), Image.ANTIALIAS)


    # 创建一个 Path 对象来表示 '1.bmp' 文件的路径
    bmp_file_path = Path('1.bmp')

    # 检查 '1.bmp' 文件是否存在于当前目录中
    if bmp_file_path.is_file():
        # 如果存在 '1.bmp' 文件，就删除它
        bmp_file_path.unlink()
    else:
        # 如果不存在 '1.bmp' 文件，就在终端中显示一条信息
        print("1.bmp 文件不存在。")
        
    # Save the grayscale image
    resized_img.save('1.bmp',format='BMP')

# 写入单独日志import datetime
import datetime
def wTxt(fPath,content):    
    current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    env_f=open(fPath,mode="a")                            
    env_f.write(f"{current_time} - {content}\n")
    env_f.close #写入结束


if __name__=="__main__":

    # 检查是否有足够的参数传递给脚本
    if len(sys.argv) > 1:
        # sys.argv[1] 将是传递给脚本的第一个参数
        # first_argument = sys.argv[1]
        # print("第一个参数是:", first_argument)
        pass
        # color2bw('sample-color.bmp')
        color2bw(pic_path,scale_factor)
                # 使用subprocess.Popen执行，包括参数，并捕获输出
        with subprocess.Popen( args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) as process:
            stdout, stderr = process.communicate()

        # 打印执行结果
        print("STDOUT:", stdout)
        print("STDERR:", stderr)
        wTxt('printer.log',stdout+stderr)
        
    else:
        print("没有提供参数。")

    # color2bw('sample-color.bmp')
    # color2bw(sys.argv[1])
    
